package com.gree.scmservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gree.common.annotation.Log;
import com.gree.common.entity.ReturnVo;
import com.gree.common.enums.BusinessType;
import com.gree.scmservice.entity.smbse.SysRoleMo;
import com.gree.scmservice.service.GscmRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 角色管理
 * Created by 180686 on 2021/3/16 20:22
 */


@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    GscmRoleService gscmRoleService;

    /**
     * 分页查询
     * @param sysRoleMo
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/list")
    public ReturnVo list(SysRoleMo sysRoleMo, HttpServletRequest httpServletRequest){
        Integer page = Integer.valueOf(httpServletRequest.getParameter("pageNum"));
        Integer pagesize = Integer.valueOf(httpServletRequest.getParameter("pageSize"));
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(sysRoleMo));
        jsonObject.put("page",page);
        jsonObject.put("pagesize",pagesize);
        return new ReturnVo<>("200","success",gscmRoleService.selectRoleList(jsonObject));
    }

    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping(value = "/{roleKey}")
    public ReturnVo getInfo(@PathVariable String roleKey)
    {
        return new ReturnVo("200","success",gscmRoleService.selectRoleByRoleKey(roleKey));
    }

    /**
     * 添加角色
     * @param sysRoleMo
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ReturnVo add(@Validated @RequestBody SysRoleMo sysRoleMo)
    {
        sysRoleMo.setPano("MENU");
        if (gscmRoleService.checkRoleKeyUnique(sysRoleMo))
        {
            return new ReturnVo("500","新增角色'" + sysRoleMo.getRoleName() + "'失败，角色权限已存在");
        }
        int resultNum = gscmRoleService.insertRole(sysRoleMo);
        if(resultNum > 0){
            return new ReturnVo("200","success");
        }else{
            return new ReturnVo("500","fail");
        }
    }

    /**
     * 修改保存角色
     */
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ReturnVo edit(@Validated @RequestBody SysRoleMo sysRoleMo)
    {
        sysRoleMo.setRtyp(1);
        sysRoleMo.setPano("MENU");
        /*if (gscmRoleService.checkRoleKeyUnique(sysRoleMo))
        {
            return new ReturnVo("500","修改角色'" + sysRoleMo.getRoleName() + "'失败，角色权限已存在");
        }*/
        int resultNum = gscmRoleService.updateRole(sysRoleMo);
        if(resultNum > 0){
            return new ReturnVo("200","success");
        }else{
            return new ReturnVo("500","fail");
        }
    }

    /**
     * 删除角色
     */
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public ReturnVo remove(@PathVariable String[] roleIds)
    {
        int resultNum = gscmRoleService.deleteRoleByIds(roleIds);
        if(resultNum > 0){
            return new ReturnVo("200","success");
        }else{
            return new ReturnVo("500","fail");
        }
    }
}
